package com.interview.item.javabase.codeDemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
 * @Author:zxh
 * @Description: 二分查找
 * @Date: 2020/3/24
 * @Modified By:
 */
public class TwoSeacher {
    private static Logger  log= LoggerFactory.getLogger(TwoSeacher.class);

    static void twoSeacher(){
        int[] ints = {23, 24, 67, 45, 3,    5, 6, 1,123, 90};
        Arrays.sort(ints);
        for (int i = 0; i < ints.length; i++) {
            log.info(ints[i]+"");
        }
        log.info("输出：{}",seacher(ints,123));
    }

    public static int seacher(int[] arr,int key){
        System.out.println("输出："+arr.length);
        int start = 0;
        int end = arr.length -1;
        while (start <= end){
            int middle = (start + end) / 2;
            if (key < arr[middle]){
                end = middle -1;
            }else  if (key > arr[middle]){
                start = middle + 1;
            }else {
                return middle;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        twoSeacher();
    }
}
